package utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.regex.Pattern;

public class SimpleText {
	private String text = new String();

	public SimpleText() {

	}

	public SimpleText(String text) {
		this.text = text;
	}

	public String getText() {
		return text;
	}

	public void readFromFile(File file) {
		FileInputStream fstream;
		try {
			fstream = new FileInputStream(file);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					fstream));
			String string = in.readLine();
			while (string != null) {
				if (System.getProperty("os.name").contains("Windows")) {
					text = text.concat(string + "\r\n");
				} else {
					text = text.concat(string + "\n");
				}
				string = in.readLine();
			}
			fstream.close();
		} catch (FileNotFoundException e) {
			System.out.println("Trying to read from " + file.toString());
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void saveToFile(File file) {
		FileOutputStream fstream;
		try {
			fstream = new FileOutputStream(file);
			fstream.write(text.getBytes());
			fstream.close();
		} catch (FileNotFoundException e) {
			System.out.println("Trying to trying to write to "
					+ file.toString());
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void saveToFile(String text, String location) {
		FileOutputStream fstream;
		File file = new File(location);
		try {
			fstream = new FileOutputStream(file);
			fstream.write(text.getBytes());
			fstream.close();
		} catch (FileNotFoundException e) {
			System.out.println("Trying to trying to write to "
					+ file.toString());
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public boolean similarTo(String query) {
		StringTokenizer stk = new StringTokenizer(query);
		Vector<String> queryWords = new Vector<String>();
		boolean flag = false;
		while (stk.hasMoreTokens()) {
			queryWords.add(stk.nextToken());
		}
		for (String queryWord : queryWords) {
			if (text.contains(queryWord)) {
				flag = true;
			}
		}
		return flag;
	}

	public boolean hasExact(String query) {
		return text.contains(query);
	}

	public boolean hasExactIgnoreCase(String query) {
		return text.toLowerCase().contains(query.toLowerCase());
	}

	public static String[] toSentences(String text) {
		String regex = "(?<=[a-z)][.!?'\"])\\s*((?=[A-Z(\"]))";
		String[] sentences = Pattern.compile(regex).split(text);
		for (String sentence : sentences) {
			SimpleText sentenceText = new SimpleText(sentence);
			if (sentenceText.getText().contains("\n")) {
				System.err.println("!!!!!!!!!!!!!!!!!!!!!Found !!!!");
			}
			sentenceText.replaceString("\r", " ");
			sentenceText.replaceString("\n", " ");
			sentenceText.removeString("  ");
			sentenceText.removeString("	");
			sentence = sentenceText.getText();
		}
		return sentences;
	}

	public String getMatch(String regex) {
		// Split sentences
		String[] sentences = Pattern.compile(
				"(?<=[a-z)][.!?'\"])\\s*((?=[A-Z(\"]))").split(text);

		// Grab matches
		String snippet = "";
		for (String str : sentences) {
			if (Pattern.compile(regex).matcher(str).matches()) {
				snippet = snippet.concat(str + ". ");
			}
		}
		return snippet;
	}

	public void setText(String text) {
		this.text = text;
	}

	public void removeString(String toRemove) {
		while (text.contains(toRemove)) {
			text = text.replace(toRemove, "");
		}
	}

	public void replaceString(String toMatch, String toReplace) {
		while (text.contains(toMatch)) {
			text = text.replace(toMatch, toReplace);
		}
	}
}
